package com.jill.utils;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Mail{
    public static void sendMail(String mail,String validCode){
        Message message=null;// 发送的信息

        Session session=null;// 构建会话

        Properties props=null;// 邮件发送配置属性

        props=new Properties();

        props.put("mail.smtp.host","smtp.163.com");

        props.put("mail.smtp.port","25");

        props.put("mail.smtp.auth",true);

        // 创建会话
        session=Session.getDefaultInstance(props,new MyAuthenticator("13929944553@163.com","UWXqVZK7CG2M9j4d"));

        message=new MimeMessage(session);
        try{
            message.setSubject("Matrix-Code验证码");

            message.setText(validCode);

            message.setFrom(new InternetAddress("13929944553@163.com"));

            message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail));

            Transport.send(message);//send
        }catch (MessagingException e){
            throw new RuntimeException(e);
        }
    }

}
